﻿Public Class User
    Public l As Int16 = 0
    Public u As Int16 = 0
    Private Sub btnnew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnew.Click
        Dim frm As New AddUser
        frm.ShowDialog()

    End Sub

    Private Sub User_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loaduser()
    End Sub
    Private Sub loaduser()
        cbouser.DisplayMember = "Username"
        cbouser.ValueMember = "User_Id"
        cbouser.DataSource = cmd.ExecuteDataTable("SELECT User_id,Username FROM User_Tbl")
    End Sub

    Private Sub btnok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click
        Dim a, b, c, d As Int16
        a = 0
        b = 0
        c = 0
        d = 0
        If cbemp.Checked Then
            a = 1
        End If
        If cbadjust.Checked Then
            b = 1
        End If
        If cbcan.Checked Then
            c = 1
        End If
        If cbadmin.Checked Then
            d = 1
        End If

        cmd.Parameters("@uu") = cbouser.SelectedValue
        Dim cc As Int16 = cmd.ExecuteScalar("SELECT COUNT(*) FROM User_role_user WHERE User_id=@uu")
        cmd.Parameters("@id") = cbouser.SelectedValue
        cmd.Parameters("@memp") = a
        cmd.Parameters("madjust") = b
        cmd.Parameters("@mcan") = c
        cmd.Parameters("madmin") = d
        If l = 0 Then
            If cc > 0 Then
                MessageBox.Show("This Employee Already Assign Special Role")
                Exit Sub
            End If
            cmd.ExecuteNonQuery("INSERT INTO User_role_user values(@id,@memp,@madjust,@mcan,@madmin)")
            MessageBox.Show("Add New User Role Sucess")
        Else
            cmd.Parameters("@uid") = l
            cmd.ExecuteNonQuery("UPDATE User_role_user set User_id=@id,Employee=@memp,Adjust=@madjust,Candadate=@mcan,Admin=@madmin WHERE User_role_user_id=@uid")
            MessageBox.Show("Add New User Role Sucess")
        End If
        
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub
End Class